#include "page3_widget.h"
#include "user/welcomewidget.h"

Page3_Widget::Page3_Widget(WelcomeWidget *widget)
{
    this->welcomewidget = widget;
    welcome_ui = this->welcomewidget->ui;

    m_tuya_iot_qthread = nullptr;

    connect(welcome_ui->pb_iot_connect,SIGNAL(clicked()),this,SLOT(on_pb_iot_connect_clicked()));     //建立信号槽
    connect(welcome_ui->pb_msg_clear,SIGNAL(clicked()),this,SLOT(on_pb_msg_clear_clicked()));     //建立信号槽

    display_pairing_qrcode(true);

    iot_event_timer = new QTimer();

    if(iot_event_timer != nullptr)
    {
        connect(iot_event_timer,SIGNAL(timeout()),this,SLOT(iot_timer_timeout_handler()));
        iot_event_timer->setInterval(1);
        iot_event_timer->start();
    }
}

Page3_Widget::~Page3_Widget()
{

}


void Page3_Widget::on_pb_iot_connect_clicked()
{
    m_tuya_iot_qthread = new Tuya_IoT_Kit_Qthread();

    if(m_tuya_iot_qthread != nullptr)
    {
        m_tuya_iot_qthread->start();
    }
}

void Page3_Widget::on_pb_msg_clear_clicked()
{
    welcome_ui->plainTextEdit_iot_data_display->clear();
}

void Page3_Widget::iot_dps_handler(void)
{
    if((iot_data.handle_flag & 0xFFFF) == FLAG_2)
    {
        if(iot_data.motor_ctrl)
        {
            display_iot_msg(QString("远程开关：打开"));
        }
        else
        {
            display_iot_msg(QString("远程开关：关闭"));
        }
        emit signal_motor_ctrl(iot_data.motor_ctrl);
        iot_data.handle_flag &= ~FLAG_2;
    }

    if((iot_data.handle_flag & 0xFFFF) == FLAG_3)
    {
        if(iot_data.light_ctrl)
        {
            display_iot_msg(QString("灯光开关：打开"));
        }
        else
        {
            display_iot_msg(QString("灯光开关：关闭"));
        }
        emit signal_light_ctrl(iot_data.light_ctrl);
        iot_data.handle_flag &= ~FLAG_3;
    }
}

void Page3_Widget::iot_event_handler(void)
{
    //display_iot_msg(QString(EVENT_ID2STR(event_msg.id)));

    switch(event_msg.id)
    {
        case TUYA_EVENT_BIND_START:
        {

        }break;

        case TUYA_EVENT_MQTT_CONNECTED:
        {
            //qDebug() << ">>> user_event_handler_on >>> TUYA_EVENT_MQTT_CONNECTED >>>";
            //display_pairing_qrcode(true);
            display_iot_msg(QString("成功连接涂鸦云服务器！"));
            welcome_ui->label_iot_state->setText(tr("连接成功"));
            iot_data.handle_flag &= ~FLAG_1;
        }break;

        case TUYA_EVENT_MQTT_DISCONNECT:
        {
            //qDebug() << ">>> user_event_handler_on >>> TUYA_EVENT_MQTT_DISCONNECT >>>";
            //display_pairing_qrcode(false);
            display_iot_msg(QString("连接断开，请检测设备网络！"));
            welcome_ui->label_iot_state->setText(tr("连接断开"));
            iot_data.handle_flag &= ~FLAG_0;
        }break;

        case TUYA_EVENT_DP_RECEIVE:
        {
            iot_dps_handler();
        }break;

        default:break;
    }
}

void Page3_Widget::iot_data_update_handler()
{
    if(iot_data.cloud_state == CLOUD_CONNECT)
    {
        //Tuya_IoT_Kit_Qthread::tuya_iot_report_bool_value((char*)DP_MOTOR_CTRL, true);
    }
}

void Page3_Widget::iot_timer_timeout_handler()
{
    //qDebug() << ">>> iot_timer_timeout_handler >>>";
    static int count = 0;

    if(iot_data.handle_flag)
    {
        iot_event_handler();
        //iot_data.handle_flag = false;
    }

    if(++count > 100)
    {
        count = 0;
        iot_data_update_handler();
    }
}

void Page3_Widget::display_iot_msg(QString str)
{
    welcome_ui->plainTextEdit_iot_data_display->appendPlainText(str);
}

void Page3_Widget::display_pairing_qrcode(bool flag)
{
    QPixmap qr_pixmap;
    QString str;

    int label_width = welcome_ui->label_pair_qrcode->width()-15;
    int label_height = welcome_ui->label_pair_qrcode->height()-15;

    if(flag) str = "https://smartapp.tuya.com/s/p?p=nqvxq8qu6heyla1e&uuid=uuid68ad0d8c1c3c944a&v=2.0";
    else str = "   ";

    encode_qrcode(str, qr_pixmap, 2);

    qr_pixmap = qr_pixmap.scaled(QSize(label_width,label_height), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);

    welcome_ui->label_pair_qrcode->setPixmap(qr_pixmap);
}

void Page3_Widget::encode_qrcode(const QString &text, QPixmap &qr_pixmap, int scale)
{
    if(text.isEmpty())return;

    QRcode *qrcode = nullptr;

    qrcode = QRcode_encodeString(text.toStdString().c_str(), 2, QR_ECLEVEL_Q, QR_MODE_8, 1);

    if(qrcode == nullptr)return;

    int qrcode_width = qrcode->width > 0 ? qrcode->width : 1;
    int width = scale * qrcode_width;
    int height = scale * qrcode_width;

    QImage image(width, height, QImage::Format_ARGB32_Premultiplied);

    QPainter painter(&image);
    QColor background(Qt::white);

    painter.setBrush(background);
    painter.setPen(Qt::NoPen);
    painter.drawRect(0,0,width,height);

    QColor foreground(Qt::black);
    painter.setBrush(foreground);

    for(int y=0; y<qrcode_width; ++y)
    {
        for(int x=0; x<qrcode_width; ++x)
        {
            unsigned char character = qrcode->data[y*qrcode_width+x];
            if(character & 0x01)
            {
                QRect rect(x*scale, y*scale, scale, scale);
                painter.drawRects(&rect, 1);
            }
        }
    }

    qr_pixmap = QPixmap::fromImage(image);
    QRcode_free(qrcode);
}


